import {getMember} from '../../../sers/member/member';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      member:{},
      femaleChk:false,
      maleChk:true,
      tjCode:'',
      birthday:'',
      memberName:'',
      phone:'',
      address:'',
      memberSex:'男',
      tittop:app.globalData.tittop+'rpx',
       lbtop:app.globalData.lbtop+'rpx',
       isWindows : app.globalData.isWindows
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("user")
    console.log(options)

    console.log(options == '')
    console.log(options != '')

    console.log(decodeURIComponent(options))
    if(options == ''){
        console.log("9999")
        let memInfo = JSON.parse(decodeURIComponent(options.memInfo));
        // getMember(memInfo.mobile).then(mem=>{
          this.setData({member:memInfo,
                        birthday:memInfo.birthday,
                        memberName:memInfo.memberName,
                        phone:memInfo.mobile,
                        address:memInfo.address,
                        memberSex:memInfo.memberSex,
                        tjCode:memInfo.rcode});
          if(memInfo.memberSex === '女'){
            this.setData({maleChk:false,femaleChk:true});
          }else{
            this.setData({maleChk:true,femaleChk:false});
          }
    }else{
        return
    }
   
    // }).catch(error=>{
    //   console.log(error);
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onInputMemberName(e){
    this.setData({memberName:e.detail.value});
  },

  bindBirthday(e){
    this.setData({birthday:e.detail.value});
  },

  onInputMobile(e){
    this.setData({phone:e.detail.value});
  },

  onInputAddress(e){
    this.setData({address:e.detail.value});
  },
  radioChange(e){
    if(e.detail.value === '女'){
      this.setData({maleChk:false,femaleChk:true});
    }else{
      this.setData({maleChk:true,femaleChk:false});
    }
    this.setData({memberSex:e.detail.value});
  },

  onInputTjCode(e){
    this.setData({tjCode:e.detail.value});
  },
  saveMember(e){
    // wx.showToast({
    //   title: '保存成功',
    // });
    console.log(e.detail.value);
    var sex = '男';
    if(this.data.memberSex != ''){
      sex = this.data.memberSex;
    }
    wx.request({
      url: app.get_request_url("savemember", "user"),
      method: "POST",
      data: {
        'memberId': '',
        'memberName': this.data.memberName,
        'memberSex': sex,
        'mobile': this.data.phone,
        'address': this.data.address,
        'rcode':this.data.tjCode,
        'birthday':this.data.birthday,
      },
      dataType: "json",
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          app.showToast("保存成功");
        }else{
          app.showToast("保存失败，请稍后重试");
        }
        setTimeout(function(){ 
          wx.navigateBack({
            delta: 1
          });
        }, 1500);
        
      },
      fail: () => {
        app.showToast("服务器请求出错");
      },
    });
  },
  toBack(e){
    wx.navigateBack({delta:1});
  }
})